package com.cool.util;

import java.io.File;
import java.io.FileWriter;

/**
 * Title: FileUtil
 * Description: TODO
 *
 * @author yls
 * @created 2018/12/3 13:17
 */

public final class FileUtil {

    /**
     * 创建.java文件所在路径 和 返回.java文件File对象
     * @param outDirFile 生成文件路径
     * @param javaPackage java包名
     * @param javaClassName java类名
     * @return
     */
    public static File generateJavaFile(File outDirFile, String javaPackage, String javaClassName) {
        String packageSubPath = javaPackage.replace('.', '/');
        File packagePath = new File(outDirFile, packageSubPath);
        File file = new File(packagePath, javaClassName + ".java");
        if(!packagePath.exists()){
            packagePath.mkdirs();
        }
        return file;
    }


    public static File generateJavaFile(File outDirFile, String javaClassName) throws  Exception {
        javaClassName+=".java";
        File file = new File(outDirFile, javaClassName);
        if(!file.exists()){
            file.createNewFile();
        }
        return file;
    }

    public static void write(File file,String text) throws  Exception{
        FileWriter writer=new FileWriter(file);
        writer.write(text);
        writer.close();
    }



}
